##=============================================================================
## Main Manuscript: Figure 3
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "lme4", "sjPlot", "ordinal", "texreg", "hrbrthemes")

ipak(packages)

##---------
# Load Data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##-------------------------
# Plots for MLMs in Table 3

dat <- dat %>% 
  mutate(save_lives = factor(save_lives, levels = c(1,2,3,4)))

str(dat$save_lives)

# save lives (ordinal)
fit1 <- clmm(save_lives ~ 1 + wave + survival_mindset + per_idx_invcov + 
               fam_idx_invcov + frontline_b + displaced + sex + rus_ethnic +
               age + edu + job + ln_viina_two_months + finances  +
               (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                  adm1_en), data = dat)

sjPlot::plot_model(fit1, 
                   type = "pred", 
                   terms = c("survival_mindset", "frontline_b")) +
  labs(y = "Predicted Probability of Save Lives", 
       x = "Survival Mindset",
       title = "",
       color = "Frontline Oblast") +
  theme_bw(base_size = 20) +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        panel.spacing = unit(0.1, "lines")) +
  guides(color = guide_legend(override.aes = list(fill = NA)))

# compromise (binary)
fit2 <- glmer(compromise ~ 1 + wave + survival_mindset + per_idx_invcov + 
                fam_idx_invcov + frontline_b + ln_viina_two_months +
                displaced + sex + rus_ethnic + age + edu + job + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                   adm1_en), 
              data = dat, 
              family = binomial,
              control = glmerControl(optimizer = "bobyqa"))

sjPlot::plot_model(fit2, 
                   type = "pred", 
                   terms = c("survival_mindset", "frontline_b")) +
  labs(y = "Predicted Probability of Compromise (Binary)", 
       x = "Survival Mindset",
       title = "",
       color = "Frontline Oblast") +
  theme_bw(base_size = 20) +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        panel.spacing = unit(0.1, "lines")) +
  guides(color = guide_legend(override.aes = list(fill = NA)))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================